"ui";

const car = require('./car.js');
const jd = require('./jd.js');
//是否验证卡密
var isCheckCar = true;
ui.layout(
    <vertical h="4000">
        <horizontal>
            <text text="卡密" />
            <input id="keygen" w="200" text="" />
            <text text="起始" />
            <input id="startIndex" w="50" text="0" />
        </horizontal>
        <horizontal>
        <button id="saveParam" text="保存" />
            <button id="zqdy" text="启动" />
        </horizontal>
    </vertical>
);


//收集参数
function collectParameters() {
    var params = car.readConfig()
    //卡密
    params.keygen = ui.keygen.text()
    //起始位置
    params.startIndex = ui.startIndex.text()
    return params
}


//初始化配置数据
function initParameters() {
    try {
        //读取配置文件
        let params = car.readConfig()
        //卡密
        if (params.keygen !== "" && params.keygen !== undefined && params.keygen !== null) {
            ui.keygen.text(params.keygen)
        }
        //起始位置
        if (params.startIndex !== "" && params.startIndex !== undefined && params.startIndex !== null) {
            ui.startIndex.text(params.startIndex)
        }
        return params
    } catch (e) {
        console.log("读取参数异常:" + e)
    }
}

//开始
ui.zqdy.click(function () {
    threads.start(function () {
        if (isCheckCar) {
            if (!car.checkCar()) {
                return
            }
        }
        let params = collectParameters()
        jd.main(params)
    });
});


//保存
ui.saveParam.click(function () {
    confirm("确认要保存吗?")
        .then(clear => {
            if (clear) {
                //更新配置信息
                car.updateCardInfo(collectParameters())
            }
        });
})

//初始化配置数据
initParameters()